package com.accompany.controller;

import com.accompany.common.Result;
import com.accompany.entity.Master;
import com.accompany.service.MasterService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 大神表 前端控制器
 * </p>
 *
 * @author DC
 * @since 2024-12-26
 */
@RestController
@RequestMapping("/master")
@Tag(name = "MasterController", description = "大神管理")
public class MasterController {

    @Autowired
    private MasterService masterService;

    /**
     * 创建大神
     *
     * @param master 大神信息
     * @return Result对象，包含操作结果
     */
    @PostMapping("/create")
    @Operation(summary = "创建大神", description = "创建一个新的大神")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "大神创建成功"),
            @ApiResponse(responseCode = "400", description = "请求参数不正确")
    })
    public Result<?> createMaster(@RequestBody Master master) {
        masterService.createMaster(master);
        return Result.success();
    }

    /**
     * 删除大神
     *
     * @param id 大神ID
     * @return Result对象，包含操作结果
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除大神", description = "根据大神ID删除大神")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "大神删除成功"),
            @ApiResponse(responseCode = "404", description = "大神不存在")
    })
    public Result<?> deleteMaster(@PathVariable Long id) {
        masterService.deleteMaster(id);
        return Result.success("删除成功");
    }

    /**
     * 更新大神信息
     *
     * @param master 大神信息
     * @return Result对象，包含操作结果
     */
    @PutMapping("/update")
    @Operation(summary = "更新大神信息", description = "根据大神ID更新大神信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "大神信息更新成功"),
            @ApiResponse(responseCode = "404", description = "大神不存在")
    })
    public Result<?> updateMaster(@RequestBody Master master) {
        masterService.updateMaster(master);
        return Result.success("更新成功");
    }

    /**
     * 获取单个大神信息
     *
     * @param id 大神ID
     * @return Result对象，包含大神信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取单个大神信息", description = "根据大神ID获取大神信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "大神不存在")
    })
    public Result<Master> getMasterById(@PathVariable Long id) {
        Master master = masterService.getMasterById(id);
        return Result.success(master);
    }

    /**
     * 获取所有大神列表
     *
     * @return Result对象，包含大神列表
     */
    @GetMapping
    @Operation(summary = "获取所有大神列表", description = "获取所有大神的列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Result<List<Master>> getAllMasters() {
        List<Master> masters = masterService.getAllMasters();
        return Result.success(masters);
    }

    /**
     * 分页查询大神列表
     *
     * @param page  页码
     * @param size  每页大小
     * @return Result对象，包含分页后的大神列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询大神列表", description = "根据页码和每页大小分页查询大神列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "分页查询成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Result<Page<Master>> getPaginatedMasters(
            @Parameter(description = "页码", required = true) @RequestParam(name = "page", defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", required = true) @RequestParam(name = "size", defaultValue = "10") Integer size) {
        Page<Master> pageResult = masterService.getPaginatedMasters(page, size);
        return Result.success(pageResult);
    }
}